/*
 * comlan
 */

package com.promfy.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

import org.apache.http.conn.util.InetAddressUtils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.util.Log;



public class AndroidUtil {

	private static final String TAG = "test";

	public static void packageManager(Context context){
		ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;  
		 
		//获得系统运行的进程
		List<ActivityManager.RunningAppProcessInfo> appList1 = mActivityManager
		        .getRunningAppProcesses();
		L.d(TAG, "=============  RunningAppProcesses ================");
		for (RunningAppProcessInfo running : appList1) {
		   L.d(TAG, running.processName);
		}
		L.d(TAG, "\n");
		 
		//获得当前正在运行的service
		List<ActivityManager.RunningServiceInfo> appList2 = mActivityManager
		        .getRunningServices(100);
		L.d(TAG, "============== RunningServices  ===============");
		for (ActivityManager.RunningServiceInfo running : appList2) {
		   L.d(TAG, running.service.getClassName());
		}
		 
		L.d(TAG, "\n");
		 
		//获得当前正在运行的activity
		List<ActivityManager.RunningTaskInfo> appList3 = mActivityManager
		        .getRunningTasks(1000);
		L.d(TAG, "============== RunningTasks ===============");
		for (ActivityManager.RunningTaskInfo running : appList3) {
		   L.d(TAG, running.baseActivity.getClassName());
		}
		L.d(TAG, "\n");
		 
		//获得最近运行的应用
		List<ActivityManager.RecentTaskInfo> appList4 = mActivityManager
		        .getRecentTasks(100, 1);
		L.d(TAG, "============== RecentTasks ===============");
		for (ActivityManager.RecentTaskInfo running : appList4) {
//			L.d(TAG, running.origActivity.getClassName());
		}
		L.d(TAG, "\n");

	}
	
	public static void clearBackground(Context context){
		ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;  
		 
		List<ActivityManager.RunningAppProcessInfo> appList1 = mActivityManager
		        .getRunningAppProcesses();
		for (RunningAppProcessInfo running : appList1) {
		   String processName = running.processName;
		  // if(processName.contains("com.comlan.shareme.launcher"))
		   mActivityManager.killBackgroundProcesses(processName);
		}
	}
	
	public static String getLocalIpAddress() {
		String ip = "";
		String input = "";
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						input =  inetAddress.getHostAddress().toString();
						if (InetAddressUtils.isIPv4Address(input)) {
							//L.d(TAG, "--------------IPV4------------------: " + input);
							if(!input.trim().equals("")){
								return input;
							}
							ip = input;
						} else {
							continue;
						}
					}
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		return ip;
	}
}
